package com.ruoyi.web.controller.product;

import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.HhType;
import com.ruoyi.system.service.IHhTypeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商品类型Controller
 * 
 * @author ruoyi
 * @date 2021-06-16
 */
@RestController
@RequestMapping("/product/type")
public class HhTypeController extends BaseController
{
    @Autowired
    private IHhTypeService hhTypeService;

    /**
     * 查询商品类型列表
     */
    @PreAuthorize("@ss.hasPermi('product:type:list')")
    @GetMapping("/list")
    public TableDataInfo list(HhType hhType)
    {
        startPage();
        List<HhType> list = hhTypeService.selectHhTypeList(hhType);
        return getDataTable(list);
    }

    /**
     * 导出商品类型列表
     */
    @PreAuthorize("@ss.hasPermi('product:type:export')")
    @Log(title = "商品类型", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(HhType hhType)
    {
        List<HhType> list = hhTypeService.selectHhTypeList(hhType);
        ExcelUtil<HhType> util = new ExcelUtil<HhType>(HhType.class);
        return util.exportExcel(list, "商品类型");
    }

    /**
     * 获取商品类型详细信息
     */
    @PreAuthorize("@ss.hasPermi('product:type:query')")
    @GetMapping(value = "/{typeId}")
    public AjaxResult getInfo(@PathVariable("typeId") Long typeId)
    {
        return AjaxResult.success(hhTypeService.selectHhTypeById(typeId));
    }

    /**
     * 新增商品类型
     */
    @PreAuthorize("@ss.hasPermi('product:type:add')")
    @Log(title = "商品类型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HhType hhType)
    {
        if (hhType.getParentId()!=0 && hhType.getTypeLevel().equals("1")){
            hhType.setTypeLevel("2");
        }
        if (hhType.getParentId()==0){
            hhType.setTypeLevel("1");
        }
        int flag = hhTypeService.insertHhType(hhType);
        if (flag<=0){
            return AjaxResult.error("操作失败，可能是没有相应的父类型号");
        }
        return toAjax(flag);
    }

    /**
     * 修改商品类型
     */
    @PreAuthorize("@ss.hasPermi('product:type:edit')")
    @Log(title = "商品类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HhType hhType)
    {
        if (hhType.getParentId()!=0 && hhType.getTypeLevel().equals("1")){
            hhType.setTypeLevel("2");
        }
        if (hhType.getParentId()==0){
            hhType.setTypeLevel("1");
        }
        int flag = hhTypeService.updateHhType(hhType);
        if (flag<=0){
            return AjaxResult.error("操作失败，可能是没有相应的父类型号");
        }
        return toAjax(flag);
    }

    /**
     * 删除商品类型
     */
    @PreAuthorize("@ss.hasPermi('product:type:remove')")
    @Log(title = "商品类型", businessType = BusinessType.DELETE)
	@DeleteMapping("/{typeIds}")
    public AjaxResult remove(@PathVariable Long[] typeIds)
    {
        return toAjax(hhTypeService.deleteHhTypeByIds(typeIds));
    }
}
